package com.ctz.commonframe.ui.widget;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctz.commonframe.R;
import com.ctz.commonframe.entity.ImageBucket;
import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.ImageFetcher;


/**
 * 选择相册
 * 
 */
public class ImageBucketChooseActivity extends TitleBarActivity {
	// 相册中图片对象集合
	public static final String EXTRA_IMAGE_LIST = "image_list";
	// 相册名称
	public static final String EXTRA_BUCKET_NAME = "buck_name";
	public static final int REQUEST_CODE_SELECT_BUCHET = 10001;
	public static final int REQUEST_CODE_SELECT_IMAGE = 10002;
	public static final int RESULT_FINISH = 20001;
	public static final String KEY_SELECTED_IMAGES = "selectedImages";
	
	private ImageFetcher mHelper;
	private List<ImageBucket> bucketList = new ArrayList<ImageBucket>();
	private GridView gvBucket;
	private ImageBucketAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_bucket_choose);
		initData();
		initView();
	}

	private void initData() {
		mHelper = ImageFetcher.getInstance(this);
		bucketList = mHelper.getImagesBucketList(false);
	}

	private void initView() {
		setTitleMiddleText("选择相册");
		setTitleLeftClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
		gvBucket = (GridView) findViewById(R.id.gv_bucket);
		mAdapter = new ImageBucketAdapter(this, bucketList);
		gvBucket.setAdapter(mAdapter);

		gvBucket.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(ImageBucketChooseActivity.this, ImageChooseActivity.class);
				intent.putExtra(EXTRA_IMAGE_LIST, (Serializable) bucketList.get(position).imageList);
				intent.putExtra(EXTRA_BUCKET_NAME, bucketList.get(position).bucketName);
				startActivityForResult(intent, REQUEST_CODE_SELECT_IMAGE);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK){
			switch (requestCode) {
			case REQUEST_CODE_SELECT_IMAGE:
				if(data != null){
					List<ImageItem> list = (List<ImageItem>) data.getSerializableExtra(KEY_SELECTED_IMAGES);
					if(list != null && list.size() > 0){
						Intent intent = new Intent();
						intent.putExtra(KEY_SELECTED_IMAGES, (Serializable)list);
						setResult(RESULT_OK, intent);
						finish();
					}
				}
				break;

			default:
				break;
			}
		}else if(resultCode == RESULT_FINISH){
			setResult(RESULT_CANCELED);
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		finish();
		super.onBackPressed();
	}

	private class ImageBucketAdapter extends AbsAdapter<ImageBucket> {
		public ImageBucketAdapter(Context context, List<ImageBucket> dataList) {
			super(context, dataList, R.layout.item_bucket_list);
		}

		@Override
		public void convert(AdapterViewHolder holder, ImageBucket data) {
			ImageView ivCover = holder.getView(R.id.cover);
			TextView tvTitle = holder.getView(R.id.title);
			TextView tvCount = holder.getView(R.id.count);

			if (data.imageList != null && data.imageList.size() > 0) {
				String thumbPath = data.imageList.get(0).thumbnailPath;
				String sourcePath = data.imageList.get(0).imagePath;
				GlideApp.with(context).clear(ivCover);
				if(thumbPath != null){
					GlideApp.with(context).load(thumbPath).into(ivCover);
				}else if(sourcePath != null){
					GlideApp.with(context).load(sourcePath).into(ivCover);
				}
			} else {
				ivCover.setImageBitmap(null);
			}

			tvTitle.setText(data.bucketName);
			tvCount.setText("(" + data.count + ")");
		}
	}
}
